#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

int value = 0; //定义一个全局变量

void *Routine1(void *p)
{
    while (true)
    {
        cout << "t1 execute...." << getpid()<<"value:"<<value<<endl;
        sleep(1);
    }
}

void *Routine2(void *p)
{
    while (true)
    {
        cout << "t2 execute...." << getpid()<<"value:"<<value++<<endl;  //线程对value值进行++
        sleep(1);
        // char* s = "hello";  //该指针指向的常量字符不可修改
        // *s = 'H' ; //出错
    }
}

void *Routine3(void *p)
{
    while (true)
    {
        cout << "t3 execute...." << getpid()<<"value:"<<value<<endl;
        sleep(1);
    }
}
int main()
{
    // 定义三个线程标识符
    pthread_t t1, t2, t3;
    // 创建三个线程
    pthread_create(&t1, nullptr, Routine1, nullptr);
    pthread_create(&t2, nullptr, Routine2, nullptr);
    pthread_create(&t3, nullptr, Routine3, nullptr);

    while (true)
    {
        cout << "main execute...." << getpid()<<"value:"<<value<<endl;
        sleep(1);
    }
    return 0;
}